package com.sz.biz.common.utils;

import com.sz.biz.logistics.ord.enums.OrdServiceTypeEnum;
import com.sz.biz.logistics.ord.service.WaybillNumberGenerator;
import com.sz.common.base.logger.Logger;
import com.sz.common.base.utils.UUIDGenerator;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Function: 订单工具类 <br>
 * Author: hpeng.wang. <br>
 * Date: 2017-05-08 10:33:00.
 */
public class OrderMake {

    public static Logger logger = Logger.getLogger(OrderMake.class);

    /**
     * 客户系统草稿状态单号的生成规则
     *
     * @return
     */
    public static synchronized String generateDraftWaybillNo() {

        String waybillNo = generateOrderNo();
        return "CG_" + waybillNo;
    }

    /**
     * 运营系统单号的生成
     * @return
     */

    public static synchronized String generateOrderNo() {
        String waybillNo = UUIDGenerator.generateOrderedUUID();
        return waybillNo;
    }

    /**
     * @param ordServiceTypeEnum 1、物流订单
     * @return
     * @throws Exception
     */
    public static synchronized String generateOrderWaybillNo(OrdServiceTypeEnum ordServiceTypeEnum) throws Exception {
        ;
        return WaybillNumberGenerator.generateWaybillNumber(ordServiceTypeEnum);
    }

    /**
     * 子单号生成
     *
     * @param cusId
     * @return
     */
    public static synchronized String generateOrderSubOrderNo(Integer cusId) {
        // 最终生成的订单号
        String finOrderNum = "";
        try {
            // 取系统当前时间作为订单号变量前半部分，精确到毫秒
            String nanoTime = System.nanoTime() + "";
            String subNanoTime = nanoTime.substring(nanoTime.length() - 5);
            long nowLong = Long.parseLong(new SimpleDateFormat("yyMMddHHmmss").format(new Date()));
            finOrderNum = "ZD" + nowLong + String.format("%05d", cusId) + subNanoTime;
            return finOrderNum;
        } catch (Exception e) {
            logger.error("", e);
            e.printStackTrace();
        }
        return finOrderNum;
    }

    /**
     * 客户外部单号
     *
     * @return
     */
    public static synchronized String generateCusExternalNo() {
        String orderNum = generateOrderNo();
        return "E" + orderNum;
    }

    public static List<String> createN(String EMSNo, int num) {
        System.out.println(EMSNo);
        List<String> EMSres = new ArrayList<String>();

        String fri;
        int res, num3, num4, num5, num6, num7, num8, num9, num0, mid;

        fri = EMSNo.substring(2, 10);
        for (int i = 0; i < num; i++) {
            num3 = Integer.parseInt(fri.substring(0, 1));
            num4 = Integer.parseInt(fri.substring(1, 2));
            num5 = Integer.parseInt(fri.substring(2, 3));
            num6 = Integer.parseInt(fri.substring(3, 4));
            num7 = Integer.parseInt(fri.substring(4, 5));
            num8 = Integer.parseInt(fri.substring(5, 6));
            num9 = Integer.parseInt(fri.substring(6, 7));
            num0 = Integer.parseInt(fri.substring(7, 8));

            mid = 8 * num3 + 6 * num4 + 4 * num5 + 2 * num6 + 3 * num7 + 5 * num8 + 9 * num9 + 7 * num0;
            res = 11 - (mid) % (11);

            if (res == 10) {
                res = 0;
            }
            if (res == 11) {
                res = 5;
            }
            //1、此处长度不足时前面需要根据情况补0  2、起始的EC 和CS请自行截取后拼接
            EMSres.add("EC" + fri + String.valueOf(res) + "CS");
            fri = String.valueOf(Long.parseLong(fri) + 1);

        }

        return EMSres;
    }

}
